﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    public partial class fanli_adminlog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //SessionHelp.AssertLogin(this); //登录验证
            
            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            switch (cmd)
            {
                case "add": initAddForm();break;
                case "edit":initEditFrom();break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            showTheRightRegion(cmd);
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            fanli.DAL.fanli_adminlog dal = new fanli.DAL.fanli_adminlog();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
            #endregion

            #region 查询条件
            string strId = txtQryId.Text.Trim();
            if (strId != "" && strId != null )
            {
                strWhere += " and id=@id";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@id", strId));
            } 
            string strLogtype = txtQryLogtype.Text.Trim();
            if (strLogtype != "" && strLogtype != null )
            {
                strWhere += " and logtype=@logtype";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@logtype", strLogtype));
            } 
            //string strAdminid = txtQryAdminid.Text.Trim();
            //if (strAdminid != "" && strAdminid != null )
            //{
            //    strWhere += " and adminid=@adminid";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@adminid", strAdminid));
            //} 
            //string strOperateTime = txtQryOperateTime.Text.Trim();
            //if (strOperateTime != "" && strOperateTime != null )
            //{
            //    strWhere += " and operateTime=@operateTime";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@operateTime", strOperateTime));
            //} 
            //string strOperateIP = txtQryOperateIP.Text.Trim();
            //if (strOperateIP != "" && strOperateIP != null )
            //{
            //    strWhere += " and operateIP=@operateIP";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@operateIP", strOperateIP));
            //} 
            //string strOperateResult = txtQryOperateResult.Text.Trim();
            //if (strOperateResult != "" && strOperateResult != null )
            //{
            //    strWhere += " and operateResult=@operateResult";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@operateResult", strOperateResult));
            //} 
            //string strExt = txtQryExt.Text.Trim();
            //if (strExt != "" && strExt != null )
            //{
            //    strWhere += " and ext=@ext";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext", strExt));
            //} 
            #endregion

             #region 分页控件绑定
             int pageSize = 30; //分页每页记录个数
             int totalRecourd = 0; //总记录个数
             int currentPage = 1; //当前页码
             try { currentPage = int.Parse(Request.QueryString["p"]); }
             catch { }

             totalRecourd = dal.GetCount(strWhere, paraList);
             dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
             if (dset!=null && dset.Tables[0].Rows.Count > 0)
             {
                 rptInfoList.DataSource = dset;
                 rptInfoList.DataBind();
                 litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
             }
             else
             {
                 litPromptMessage.Text = "没有查询到相关记录";
             } 
             #endregion
             #region 单页数据绑定
             //strWhere += " " + strOrderBy;
             //dset = dal.GetDataset(strWhere, paraList, dataFields);
             //if (dset.Tables[0].Rows.Count > 0)
             //{
             //    rptInfoList.DataSource = dset;
             //    rptInfoList.DataBind();
             //}
             //else
             //{
             //    litPromptMessage.Text = "没有查询到相关记录";
             //} 
             #endregion

        }
        #endregion

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = true;
            int varId ;	//id
            string varLogtype;	//日志类型
            int varAdminid ;	//管理员id
            DateTime varOperateTime ;	//操作时间
            string varOperateIP;	//操作ip
            string varOperateResult;	//操作结果
            string varExt;	//扩展
            try
            {
               varId =int.Parse(txtId.Text.Trim());	//id
               varLogtype =txtLogtype.Text.Trim();	//日志类型
               varAdminid =int.Parse(txtAdminid.Text.Trim());	//管理员id
               varOperateTime =DateTime.Parse(txtOperateTime.Text.Trim());	//操作时间
               varOperateIP =txtOperateIP.Text.Trim();	//操作ip
               varOperateResult =txtOperateResult.Text.Trim();	//操作结果
               varExt =txtExt.Text.Trim();	//扩展
            }
            catch
            {
               LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
               return;
            }
            if (varId != 0) isAdd = false;
            #endregion

            #region 赋值model
            fanli.Model.fanli_adminlog model = new fanli.Model.fanli_adminlog();
            model.id = varId;	//id
            model.logtype = varLogtype;	//日志类型
            model.adminid = varAdminid;	//管理员id
            model.operateTime = varOperateTime;	//操作时间
            model.operateIP = varOperateIP;	//操作ip
            model.operateResult = varOperateResult;	//操作结果
            model.ext = varExt;	//扩展
            #endregion

            #region 执行操作
            fanli.DAL.fanli_adminlog dal = new fanli.DAL.fanli_adminlog();
            if (isAdd)
            {
                if (dal.Add(model) > 0)
                {
                    
                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {
                    
                    LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
                }
            }
            else
            {
                if (dal.Update(model) > 0)
                {
                    
                    LIB.JsWrite.AlertAndGo(this, "修改成功", "?cmd=list");
                }
                else
                {
                    
                    LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
                }
            } 
            #endregion
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                //litPromptMessage.Text = "非法的参数";
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            fanli.DAL.fanli_adminlog dal = new fanli.DAL.fanli_adminlog();
            if (dal.Delete(id)>0)
            {
                //litPromptMessage.Text = "删除成功";
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {
                
                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            } 
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                txtLogtype.Text = "";
                txtAdminid.Text = "";
                txtOperateTime.Text = "";
                txtOperateIP.Text = "";
                txtOperateResult.Text = "";
                txtExt.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id =Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_adminlog model = new fanli.Model.fanli_adminlog();
                fanli.DAL.fanli_adminlog dal = new fanli.DAL.fanli_adminlog();
                model = dal.GetModel(id);

                txtId.Text = model.id.ToString();
                txtLogtype.Text = model.logtype.ToString();
                txtAdminid.Text = model.adminid.ToString();
                txtOperateTime.Text = model.operateTime.ToString();
                txtOperateIP.Text = model.operateIP.ToString();
                txtOperateResult.Text = model.operateResult.ToString();
                txtExt.Text = model.ext.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_adminlog model = new fanli.Model.fanli_adminlog();
                fanli.DAL.fanli_adminlog dal = new fanli.DAL.fanli_adminlog();
                model = dal.GetModel(id);

                lblLogtype.Text = model.logtype.ToString();
                lblAdminid.Text = model.adminid.ToString();
                lblOperateTime.Text = model.operateTime.ToString();
                lblOperateIP.Text = model.operateIP.ToString();
                lblOperateResult.Text = model.operateResult.ToString();
                lblExt.Text = model.ext.ToString();
            }
        }

        #endregion

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion



    }


}

